package com.wuzhun.wxmp.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.Calendar;
import java.util.Date;

import static org.apache.commons.lang3.time.DateUtils.addDays;
import static org.apache.commons.lang3.time.DateUtils.truncate;

/**
 * 日期时间工具类
 *
 * @author renbaolin
 * @date 2022-12-05 10:34 AM
 */
public class DateTimeUtil {

    public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static final String DATE_PATTERN = "yyyy-MM-dd";
    /**
     * ri
     *
     * @param day
     * @return
     */
    public static Date getDateByIncreaseDayOld(int day) {
        long time = System.currentTimeMillis();
        time += (long) day * 24 * 3600 * 1000;
        return new Date(time);
    }

    /**
     * 当前时间加固定上天数
     *
     * @param day
     * @return
     */
    public static Date getDateByIncreaseDay(int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, day);
        return calendar.getTime();
    }

    /**
     * 当前时间加固定上分钟
     *
     * @param minute
     * @return
     */
    public static Date getDateByIncreaseMinute(int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTime();
    }

    public static Date today() {
        return truncate(new Date(), 5);
    }

    public static Date yesterday() {
        return addDays(today(), -1);
    }

    public static Date getDateStartTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return truncate(calendar, 5).getTime();
    }

    public static Date getDateEndTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        Calendar truncateCalendar = truncate(calendar, 5);
        truncateCalendar.add(5, 1);
        truncateCalendar.add(14, -1);
        return truncateCalendar.getTime();
    }

    /**
     * 处理时间
     *
     * @param joinPartyTime
     * @return
     */
    public static String handleTime(String joinPartyTime) {
        return StringUtils.replace(joinPartyTime, ".", "-");
    }

    /**
     * 获取年月日
     *
     * @param date
     */
    public static String getYearMonthDay(Date date) {
        if (date == null) {
            date = new Date();
        }
        return new SimpleDateFormat(DATE_PATTERN).format(date);
    }

    /**
     * 获取当前年
     */
    public static int getCurrentYear() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.YEAR);
    }

    /**
     * 获取当前月份
     *
     * @return
     */
    public static int getCurrentMonth() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.MONTH) + 1;
    }

    /**
     * 根据格式获取日期
     *
     * @param dayFormat
     * @return
     */
    public static String getFormatDay(String dayFormat) {
        if (StringUtils.isEmpty(dayFormat)) {
            return "";
        }
        Date date = new Date();
        return new SimpleDateFormat(dayFormat).format(date);
    }

    /**
     * 格式化时间
     *
     * @param dateTime
     * @return
     */
    public static String getFormatDateTime(Date dateTime) {
        if (dateTime == null) {
            dateTime = new Date();
        }
        return new SimpleDateFormat(DATE_TIME_PATTERN).format(dateTime);
    }

    /**
     * 时间类型转换
     *
     * @param str
     * @return
     */
    public static Date string2Date(String str) {
        if (StringUtils.isBlank(str)) {
            return null;
        }

        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_PATTERN);
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 设置当天的开始时间
     *
     * @param calendar
     */
    private static void setMinTimeOfDay(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }

    /**
     * 设置当天的结束时间
     *
     * @param calendar
     */
    private static void setMaxTimeOfDay(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.MILLISECOND, 999);
    }


    /**
     * 获取指定月的开始时间
     *
     * @return
     */
    public static Date getStartTimeOfCurrentMonth(Date date) {
        if (date == null) {
            date = new Date();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
        setMinTimeOfDay(calendar);
        return calendar.getTime();
    }

    /**
     * 获取指定月的结束时间
     *
     * @return
     */
    public static Date getEndTimeOfCurrentMonth(Date date) {
        if (date == null) {
            date = new Date();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        int maxMonthDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), maxMonthDay);
        setMaxTimeOfDay(calendar);
        return calendar.getTime();
    }

    public static void main(String[] args) {
        System.out.println(getDateStartTime(new Date()));
        System.out.println(getDateEndTime(new Date()));
    }
}
